<?php
include_once '..//util/arrayutil.php';
include_once 'Category.php';
class Product {

    private $id;
    private $code;
    private $name;
    private $description;
    private $price;
    private $category;

    public function setId($_id) {
        $this->id = $_id;
    }

    public function getId() {
        return $this->id;
    }

    public function setCode($_code) {
        $this->code = $_code;
    }

    public function getCode() {
        return $this->code;
    }

    public function setName($_name) {
        $this->name = $_name;
    }

    public function getName() {
        return $this->name;
    }

    public function setDescription($_des) {
        $this->description = $_des;
    }

    public function getDescription() {
        return $this->description;
    }

    public function setPrice($_price) {
        $this->price = $_price;
    }

    public function getPrice() {
        return $this->price;
    }

    public function setCategory($_cat) {
        if ($_cat instanceof Category) {
            $this->category = $_cat;
        }
    }

    public function getCategory() {
        return $this->category;
    }

    public function save() {
        $category_id = $this->category->getId();
        mysql_query("insert into product(name,description,code,price,category_id) 
                values('$this->name','$this->description','$this->code',$this->price,$category_id)");
    }

    public function delete() {
        mysql_query("delete from product where id=$this->id");
    }

    public static function load($id) {
        $result = mysql_query("select id,code,name,description,price,category_id from product where id=$id")
                or die(mysql_error());
        $row = mysql_fetch_row($result);
        $product = new Product();
        $product->id = $row[0];
        $product->code = $row[1];
        $product->name = $row[2];
        $product->description = $row[3];
        $product->price = $row[4];
        $product->category = Category::load($row[5]);
        return $product;
    }

    public static function loads() {
        $result = mysql_query("select id,code,name,description,price,category_id from product");
        $products = array();
        while ($row = mysql_fetch_array($result)) {
            $product = new Product();
            $product->id = $row[0];
            $product->code = $row[1];
            $product->name = $row[2];
            $product->description = $row[3];
            $product->price = $row[4];
            $product->category = Category::load($row[5]);
            array_push($products, $product);
        }
        return $products;
    }

    public static function loadsForJSON() {
        return json_encode(ArrayUtil::arrayOfObjectToArray(Product::loads()));
    }

    public static function loadForJSON($id) {
        return json_encode(ArrayUtil::arrayOfObjectToArray(Product::load($id)));
    }

}

?>
